/* ====================================================================
 * (#)Header: WorkflowEngineJbpm.java
 * (#)Revision: 
 * (#)Date Created: Sep 30, 2011 12:47:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.workflow.jbpm;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;

import com.karolak.workflow.WorkflowEngine;

/**
 * <code>WorkflowEngineJbpm</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class WorkflowEngineJbpm implements WorkflowEngine {

	private KnowledgeBuilder knowledgeBuilder;
	private KnowledgeBase knowledgeBase;

	public WorkflowEngineJbpm() {
		initialize();
	}

	protected void initialize() {
		setKnowledgeBuilder(KnowledgeBuilderFactory.newKnowledgeBuilder());
	}

	public void loadProcess(String processFileName) {
		Resource resource = ResourceFactory.newClassPathResource(processFileName);
		getKnowledgeBuilder().add(resource, ResourceType.BPMN2);
		setKnowledgeBase(getKnowledgeBuilder().newKnowledgeBase());
	}

	public long startProcess(String processId) {
		StatefulKnowledgeSession session = getKnowledgeBase().newStatefulKnowledgeSession();
		ProcessInstance processInstance = session.startProcess(processId);
		long processInstanceId = processInstance.getId();
		session.dispose();
		return processInstanceId;
	}

	// === get/set === //
	protected KnowledgeBase getKnowledgeBase() {
		return knowledgeBase;
	}

	protected void setKnowledgeBase(KnowledgeBase knowledgeBase) {
		this.knowledgeBase = knowledgeBase;
	}

	protected KnowledgeBuilder getKnowledgeBuilder() {
		return knowledgeBuilder;
	}

	protected void setKnowledgeBuilder(KnowledgeBuilder knowledgeBuilder) {
		this.knowledgeBuilder = knowledgeBuilder;
	}
}
